package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Class1;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentAddReq;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public Boolean addStudent(Student student) {
        studentMapper.insertStudent(student);
        return true;
    }

    @Override
    public Boolean deleteByIds(String[] ids) {
        studentMapper.deleteByIds(ids);
        return true;
    }


    @Override
    public PageBean getAllStudents(Integer page, Integer pageSize, String name, String educationId,
                                   String number, String classId) {
        PageHelper.startPage(page, pageSize);

        List<Student> list = studentMapper.list(name, educationId, number, classId);
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        PageBean pageBean = new PageBean(pageInfo.getTotal(), pageInfo.getList());
        return pageBean;
    }

    @Override
    public Boolean updateStudent(StudentAddReq req) throws Exception{
        try {
            Class1 tmp = req.getStudentClass();
            Integer classId = Objects.nonNull(tmp)? tmp.getId(): null;

            Student student = new Student(req.getId(), req.getName(),req.getNumber(), req.getGender(),
                    req.getPhone(), req.getEducationId(), classId,LocalDateTime.now(),
                    null, req.getDisciplineScore()); // 只要扣的分>0，就算一次扣分次数
            studentMapper.updateStudent(student);
            return true;
        }catch (Exception e){
            // 返回值不为true即可在controller层返回Result.error("")
            System.out.println(e);
            throw new Exception();
        }
    }


}
